import java.util.*;

public class Combat extends GameStage {
  boolean backtomap = false;
  boolean ready = false;
  int turn = 0;
  int numenemies;
  int loc = 70;
  Player p1 = null;
  ArrayList<Enemy> enemies = null;
  Button b1;
  Button b2;
  Button b3;
  Button cancel;
  Player origPlayer;
  boolean selectSpell = false;
  boolean selectWeaponTarget = false;
  boolean selectSpellTarget = false;
  String pendingSpell;
  ArrayList<Button> menu;
  PImage img;

  public Combat(Player p, int ne) {
    p1 = p;
    origPlayer = p;
    menu = new ArrayList<Button>();
    enemies = new ArrayList<Enemy>();
    for (int i = 0; i < ne;i++) {
      enemies.add(new Enemy());
    }
    numenemies = ne;
    reload(numenemies);

    b1 = new Button(60, 450, 170, 110);
    b2 = new Button(320, 450, 170, 110);
    b3 = new Button(580, 450, 170, 110);
  }


  public void keyboardAct() {
  }

  public void mouseAct(int x, int y) {
    if (ready) {
      if (cancel.isClicked(x, y)) {
        reload(numenemies);

        selectSpellTarget=false;
        selectSpell=false;
        selectWeaponTarget=false;
      }
      else {
        if (selectSpellTarget == true) {
          for (Button b : menu) {
            if (b.isClicked(x, y)) {
              selectSpellTarget = false;
              System.out.println(b.who.myHP);
              p1.useAbility(pendingSpell, b.who);
              endTurn();
              reload(numenemies);

              System.out.println(b.who.myHP);
              break;
            }
          }
        }
        else {
          if (selectWeaponTarget == true) {
            int tiempo = 1;
            for (Button b : menu) {
              if (b.isClicked(x, y)) {
                selectWeaponTarget = false;
                for (int i =1; i<15; i++) 
                  if (second()==i)
                    draw();
                System.out.println(b.who.myHP);
                p1.attack(b.who);
                endTurn();
                reload(numenemies);

                System.out.println(b.who.myHP);
                break;
              }
            }
          }
          else { 
            if (selectSpell == true) {
              for (Button b : menu) {
                if (b.isClicked(x, y)) {
                  pendingSpell = b.menuItem;
                  selectSpell = false;
                  selectSpellTarget = true;
                  menu = new ArrayList<Button>();
                  fill(255);
                  img = loadImage("buttonpic.png");
                  image(img, 75, 30);
                  reload(numenemies);

                  img = loadImage("buttonpic.png");
                  for (int n=1; n<=numenemies; n++) {
                    image(img, 60, 110, img.width, img.height);
                    menu.add(new Button(60, 110, img.width, img.height, p1));
                    if (n==1 && enemies.get(0) != null) {
                      image(img, 640, 110, img.width-50, img.height);
                      menu.add(new Button(640, 110, img.width-50, img.height, enemies.get(n-1)));
                    }
                    if (n==2 && enemies.get(1) != null) {
                      image(img, 575, 110, img.width-50, img.height);
                      menu.add(new Button(575, 110, img.width-50, img.height, enemies.get(n-1)));
                    }
                    if (n==3 && enemies.get(2) != null) {
                      image(img, 510, 110, img.width-50, img.height);
                      menu.add(new Button(510, 110, img.width-50, img.height, enemies.get(n-1)));
                    }
                    if (n==4 && enemies.get(3) != null) {
                      image(img, 445, 110, img.width-50, img.height);
                      menu.add(new Button(445, 110, img.width-50, img.height, enemies.get(n-1)));
                    }
                  }

                  break;
                }
              }
            }
            else {
              if (b2.isClicked(x, y)) {
                int hor = 160;
                int ver = 50;
                int i = 0;
                AbilityRetriever ab = new AbilityRetriever();
                for (String s: p1.abilities) {
                  if (i%4 == 0 && i != 0) {
                    hor = hor + 150;
                    ver = 50;
                  }
                  fill(255);
                  if (ab.getEffect(s)[3] <= p1.myFoc) {
                    img = loadImage("buttonpic.png");
                    menu.add(new Button(hor, ver, 100, 50, s));
                  }
                  else {
                    img = loadImage("buttonpicgray.png");
                  }
                  selectSpell = true;
                  image(img, hor, ver);
                  fill(0);
                  text(s+" ("+ab.getEffect(s)[3]+")", hor+15, ver+25);
                  ver = ver + 100;
                  i++;
                }
              }
              else {
                if (b1.isClicked(x, y)) {
                  selectWeaponTarget = true;
                  menu.add(new Button(120, 150, 100, 50, p1));
                  int i = 400/enemies.size() - 75;
                  img = loadImage("buttonpic.png");
                  for (int n=1; n<=numenemies; n++) {
                    image(img, 60, 110, img.width, img.height);
                    menu.add(new Button(60, 110, img.width, img.height, p1));
                    if (n==1 && enemies.get(0) != null) {
                      image(img, 640, 110, img.width-50, img.height);
                      menu.add(new Button(640, 110, img.width-50, img.height, enemies.get(n-1)));
                    }
                    if (n==2 && enemies.get(1) != null) {
                      image(img, 575, 110, img.width-50, img.height);
                      menu.add(new Button(575, 110, img.width-50, img.height, enemies.get(n-1)));
                    }
                    if (n==3 && enemies.get(2) != null) {
                      image(img, 510, 110, img.width-50, img.height);
                      menu.add(new Button(510, 110, img.width-50, img.height, enemies.get(n-1)));
                    }
                    if (n==4 && enemies.get(3) != null) {
                      image(img, 445, 110, img.width-50, img.height);
                      menu.add(new Button(445, 110, img.width-50, img.height, enemies.get(n-1)));
                    }
                  }
                  /*for (Enemy e : enemies) {
                   image(img, 600, i);
                   menu.add(new Button(600, i, 100, 50, e));
                   i = i + (400/enemies.size());
                   }*/
                }
              }
            }
          }
        }
      }
    }
    else {
      ready = true;
    }
  }

  private void endTurn() {
    p1.checkBuffs();
    Enemy e;
    for (int x = numenemies-1; x >= 0; x--) {
      e = enemies.get(x);
      if (e != null) {
        e.checkBuffs();
        if (e.myHP <= 0) {
          enemies.set(x, null);
        }
        else {
          e.act(p1);
          p1.checkBuffs();
          e.checkBuffs();
        }
      }
    }
    if (p1.myHP <= 0) {
      backtomap = true;
      p1.myMoney = p1.myMoney / 2;
      System.out.println("Half your money was stolen while you were unconcious.");
    }
  }

  void draw() {
    img = loadImage("PlayerSprite.png");
    int time=0;
    if (loc<=630) {
      loc+=20;
    }
    else {
      reload(numenemies);
      loc=70;
    }
  }

  private void reload2(int numememies, int xy, int d) {
    img = loadImage("cave(2).png");
    background(img);
    img = loadImage("buttonpic.png");
    image(img, 5, 5, img.width/2, img.height/2);
    fill(0);
    text("Cancel", 12, 22);
    cancel = new Button(5, 5, img.width/2, img.height/2); 
    menu.add(new Button(5, 5, img.width/2, img.height/2));
    img = loadImage("PlayerSprite.png");
    image(img, xy, 170);
    int j = numenemies * 2;
    img = loadImage("PlayerSprite.png");
    Enemy sam;
    for (int x = numenemies; x > 0; x--) {
      sam = enemies.get(x-1);
      img = loadImage("enemy1.png");
      if (x==d)
        x++;
      if (x==1) {

        image(img, 635, 170, img.width/j - j/2, img.height/j - numenemies);
        //Health Bar
        fill(50, 120, 150);

        rect(660, 35, ((1.0*sam.myHP) / (1.0*sam.baseHP)) * 50.0, 10);
        System.out.println("Here");
        fill(300);
        rect(660, 35, 50, 10);
        fill(225);
        text("Health", 670, 55);
        //Focus Bar
        fill(110, 30, 190);
        rect(660, 60, ((1.0*sam.myFoc) / (1.0*sam.baseFoc)) * 50.0, 10);
        fill(300);
        rect(660, 60, 50, 10);
        fill(225);
        text("Focus", 670, 80);
      }
      if (x==2) {
        image(img, 570, 170, img.width/j + 25, img.height/j + 80);
        sam=enemies.get(1);
        //Health Bar
        fill(50, 120, 150);
        rect(590, 35, (((1.0*sam.myHP) / (1.0*sam.baseHP))) * 50.0, 10);
        fill(300);
        rect(590, 35, 50, 10);
        fill(225);
        text("Health", 600, 55);
        //Focus Bar
        fill(110, 30, 190);
        rect(590, 60, ((1.0*sam.myFoc) / (1.0*sam.baseFoc)) * 50.0, 10);
        fill(300);
        rect(590, 60, 50, 10);
        fill(225);
        text("Focus", 600, 80);
      }
      if (x==3) {
        image(img, 510, 170, img.width/j + 35, img.height/j + 100);
        sam=enemies.get(2);
        //Health Bar
        fill(50, 120, 150);
        rect(520, 35, ((1.0*sam.myHP) / (1.0*sam.baseHP)) * 50.0, 10);
        fill(300);
        rect(520, 35, 50, 10);
        fill(225);
        text("Health", 530, 55);
        //Focus Bar
        fill(110, 30, 190);
        rect(520, 60, ((1.0*sam.myFoc) / (1.0*sam.baseFoc)) * 50.0, 10);
        fill(300);
        rect(520, 60, 50, 10);
        fill(225);
        text("Focus", 530, 80);
      }
      if (x==4) {
        image(img, 450, 170, img.width/j + 40, img.height/j + 100);
        sam=enemies.get(3);
        //Health Bar
        fill(50, 120, 150);
        rect(450, 35, ((1.0*sam.myHP) / (1.0*sam.baseHP)) * 50.0, 10);
        fill(300);
        rect(450, 35, 50, 10);
        fill(225);
        text("Health", 460, 55);
        //Focus Bar
        fill(110, 30, 190);
        rect(450, 60, ((1.0*sam.myFoc) / (1.0*sam.baseFoc)) * 50.0, 10);
        fill(300);
        rect(450, 60, 50, 10);
        fill(225);
        text("Focus", 460, 80);
      }
      j-=2;
    }
    fill(50, 120, 150);
    rect(50, 35, ((1.0*p1.myHP) / (1.0*p1.baseHP)) * 100.0, 10);
    fill(300);
    rect(50, 35, 100, 10);
    fill(225);
    text("Health", 50, 55);
    //Player health bar
    fill(110, 30, 190);
    rect(50, 60, ((1.0*p1.myFoc) / (1.0*p1.baseFoc)) * 100.0, 10);
    fill(300);
    rect(50, 60, 100, 10);
    fill(225);
    text("Focus", 50, 80);
    img = loadImage("PlayerSprite.png");
  }




  private void reload(int numememies) {
    int counter = 4;
    img = loadImage("cave(2).png");
    background(img);
    img = loadImage("buttonpic.png");
    image(img, 5, 5, img.width/2, img.height/2);
    text("Cancel", 12, 22);
    fill(0);
    cancel = new Button(5, 5, img.width/2, img.height/2);
    menu.add(new Button(5, 5, img.width/2, img.height/2));
    img = loadImage("PlayerSprite.png");
    image(img, 50, 170);
    Enemy sam;
    int j = numenemies * 2;
    for (int x = numenemies; x > 0; x--) {
      sam = enemies.get(x-1);
      img = loadImage("enemy1.png");
      if (x==1 && enemies.get(0)!= null) {
        image(img, 635, 170, img.width/j - j/2, img.height/j - numenemies);
        //Health Bar
        fill(50, 120, 150);
        rect(660, 35, ((1.0*sam.myHP) / (1.0*sam.baseHP)) * 50.0, 10);
        fill(300);
        rect(660, 35, 50, 10);
        fill(225);
        text("Health", 670, 55);
        //Focus Bar
        fill(110, 30, 190);
        rect(660, 60, ((1.0*sam.myFoc) / (1.0*sam.baseFoc)) * 50.0, 10);
        fill(300);
        rect(660, 60, 50, 10);
        fill(225);
        text("Focus", 670, 80);
        counter--;
      }
      if (x==2 && enemies.get(1)!= null) {
        image(img, 570, 170, img.width/j + 25, img.height/j + 80);
        sam=enemies.get(1);
        //Health Bar
        fill(50, 120, 150);
        rect(590, 35, ((1.0*sam.myHP) / (1.0*sam.baseHP)) * 50.0, 10);
        fill(300);
        rect(590, 35, 50, 10);
        fill(225);
        text("Health", 600, 55);
        //Focus Bar
        fill(110, 30, 190);
        rect(590, 60, ((1.0*sam.myFoc) / (1.0*sam.baseFoc)) * 50.0, 10);
        fill(300);
        rect(590, 60, 50, 10);
        fill(225);
        text("Focus", 600, 80);
        counter--;
      }
      if (x==3 && enemies.get(2)!= null) {
        image(img, 510, 170, img.width/j + 35, img.height/j + 100);
        sam=enemies.get(2);
        //Health Bar
        fill(50, 120, 150);
        rect(520, 35, ((1.0*sam.myHP) / (1.0*sam.baseHP)) * 50.0, 10);
        fill(300);
        rect(520, 35, 50, 10);
        fill(225);
        text("Health", 530, 55);
        //Focus Bar
        fill(110, 30, 190);
        rect(520, 60, ((1.0*sam.myFoc) / (1.0*sam.baseFoc)) * 50.0, 10);
        fill(300);
        rect(520, 60, 50, 10);
        fill(225);
        text("Focus", 530, 80);
        counter--;
      }
      if (x==4 && enemies.get(3)!= null) {
        image(img, 450, 170, img.width/j + 40, img.height/j + 100);
        sam=enemies.get(3);
        //Health Bar
        fill(50, 120, 150);
        rect(450, 35, ((1.0*sam.myHP) / (1.0*sam.baseHP)) * 50.0, 10);
        fill(300);
        rect(450, 35, 50, 10);
        fill(225);
        text("Health", 460, 55);
        //Focus Bar
        fill(110, 30, 190);
        rect(450, 60, ((1.0*sam.myFoc) / (1.0*sam.baseFoc)) * 50.0, 10);
        fill(300);
        rect(450, 60, 50, 10);
        fill(225);
        text("Focus", 460, 80);
        counter--;
      }
      j-=2;
    }
    fill(50, 120, 150);
    rect(50, 35, ((1.0*p1.myHP) / (1.0*p1.baseHP)) * 100.0, 10);
    fill(300);
    rect(50, 35, 100, 10);
    fill(225);
    text("Health", 50, 55);
    //Player health bar
    fill(110, 30, 190);
    rect(50, 60, ((1.0*p1.myFoc) / (1.0*p1.baseFoc)) * 100.0, 10);
    fill(300);
    rect(50, 60, 100, 10);
    fill(225);
    text("Focus", 50, 80);
    img = loadImage("PlayerSprite.png");
    if (counter == 4) {
      p1.myMoney = p1.myMoney + (150*numenemies);
      p1.myXP = p1.myXP + (20*numenemies);
      backtomap = true;
    }
  }
}

